package com.example.administrator.youlu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.youlu.R;
import com.example.administrator.youlu.been.Contact;
import com.example.administrator.youlu.utils.ImageManager;

public class MyContactsAdapter extends MyBaseAdapter<Contact> {


    public MyContactsAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {



        ViewHoder viewHoder;

        if (view==null){
            view=layoutInflater.inflate(R.layout.contact_item,null);
            viewHoder=new ViewHoder();
            viewHoder.ivIcon=view.findViewById(R.id.tvIcon);
            viewHoder.tvName=view.findViewById(R.id.tvName);
            view.setTag(viewHoder);

        }else {
            viewHoder= (ViewHoder) view.getTag();
        }


        Contact contact= getItem(i);

        viewHoder.tvName.setText(contact.getName());

        if (i==0){
            viewHoder.ivIcon.setImageResource(R.mipmap.ic_add_contact);
        }else {
            if (contact.getPhoto()!=null){
                Bitmap bitmap=ImageManager.formatBitmap(mContext,contact.getPhoto());
                viewHoder.ivIcon.setImageBitmap(bitmap);
            }

            else
                viewHoder.ivIcon.setImageResource(R.mipmap.ic_launcher);
        }




        return view;
    }
    class ViewHoder{
        TextView tvName;
        ImageView  ivIcon;
    }
}
